﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using Zj.DDDSample.Domain.Events;

namespace Zj.DDDSample.Messaging.Simple
{
    public class MessageQueue
    {
        private readonly ConcurrentQueue<IEvent> _queue = new ConcurrentQueue<IEvent>();

        public event EventHandler<EventProcessedArgs> eventPushed;
        public void Push(IEvent @event)
        {
            _queue.Enqueue(@event);
            OnMessagePushed(new EventProcessedArgs(@event));
        }

    

        public IEvent Pop(IEvent @event)
        {
            var succeed = this._queue.TryDequeue(out @event);
            return succeed? @event:null;
        }

        private void OnMessagePushed(EventProcessedArgs eventProcessArgs)
        {
            this.eventPushed?.Invoke(this,eventProcessArgs);
        }

        public int Count => this._queue.Count;
    }
}
